home *** CD-ROM | disk | FTP | other *** search
- //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
- //
- // Amalgam.c
- //
- // The application uses a modeless dialog to present user with several buttons,
- // each of which performs an interesting action:
- //
- // o finds deepest screen available; since there may be more than
- // one, this simply returns the last deepest one in the GDevice list.
- // o finds the screen with the largest area; again, since there may
- // be more than one in the list with similar resolutions, this will
- // return the last, largest one it found in the GDevice list.
- // o allows you to take over an entire window; if you have more than
- // one screen available, whichever one contains a larger portion of
- // the dialog will be taken over.
- // o hide and un-hide the menubar.
- // o demonstrates fading a GDevice to black and back again.
- // o More intelligent window zooming; windows are zoomed out to whichever
- // screen the majority of their real estate is on; access of this function
- // is via the usual zoom-box; since we do not provide for window re-sizing,
- // you may re-set the size of the window by option-clicking the zoom box.
- //
- // Notes:
- //
- // + clicking in the close box of the dialog quits the program
- // + though the program lets you hide the menubar at any time, real programs
- // would only do so if they were taking over the entire main screen; to see
- // what this is like, Minimax the dialog so it fills the main screen, then
- // hide the menubar--now, click where the menubar was and it will reappear,
- // then, click elsewhere in the dialog and it will resume hiding--this is
- // what real games do.
- // + fading demonstrated here fades an entire screen. This is the most robust
- // method of fading as it avoids some problems presented by the 68KAV Macs;
- // other methods of fading are to slowly modify the device's gamma, and
- // doing a Color Table animation fade, but both of these methods may fail
- // on the AVs.
- //
- // History:
- //
- // 950305 jb: Written
- //
- //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
-
-
- // __#Defines________________________________________________________________________
- #define kAmalgamDLOGID 128
-
- #define kSleepTix 10 //defines how much time we give up if there aren't
- //any events for our app
-
- // __#Headers________________________________________________________________________
- #include "QDOffscreen.h"
- #include "Menu.h"
- #include "Amalgam.h"
- #include "OurDlog.h"
-
- // __#Protos_________________________________________________________________________
- // __ Macros_________________________________________________________________________
- // __ Enums__________________________________________________________________________
- // __ Typedefs_______________________________________________________________________
- // __ Static Protos__________________________________________________________________
- static Boolean OpenOurModelessDialog( void );
- static Boolean InitApp( void );
- static void QuitApp( void );
-
- static void EventLoop( void );
- static void HandleEvent( void );
-
- // __ Extern Globals_________________________________________________________________
- OSErr gErr;
- Str32 gAppName;
- DialogPtr gMainDlogPtr;
- CGrafPtr gMainDlogPort;
- GDHandle gMainDlogGDev;
- EventRecord gTheEvent;
- Boolean gInForeground;
- Boolean gQuitting;
-
- GDHandle gMainDevice;
- short gNumActiveScreens;
- Rect gOriginalDlogPortRect;
-
- // __ Static Globals_________________________________________________________________
- // __ Functions______________________________________________________________________
-
-
- //____ main __________________________________________________________________________
- //
- void main( void )
- {
- if (InitApp()) //Initialize managers, allocate global structures, etc…
- {
- EventLoop(); //Handle events until user quits
-
- QuitApp(); //dispose and release everything
- }
-
- }//main
-
-
- //____ InitApp __________________________________________________________________________
- //
- // Returns FALSE if there were any problems initializing, or if the System lacks
- // any amenities we require.
- //
- static Boolean InitApp( void )
- {
- ToolBoxInit();
-
- if (!EnviroCheck())
- return FALSE;
-
- gInForeground = TRUE;
-
- InitOurMenubar();
-
- StartupHideMenuBar(); //initialize stuff for hiding menu bar
-
- gMainDevice = GetMainDevice();
- gNumActiveScreens = CountAvailableDevices();
-
- GetAppName((char *)gAppName);
-
- if (!OpenOurModelessDialog())
- return FALSE;
-
- gQuitting = FALSE;
-
-
- return TRUE;
- }//InitApp
-
-
- //____ QuitApp __________________________________________________________________________
- //
- // Dispose of and release anything and everything
- //
- static void QuitApp( void )
- {
- if (NULL != gMainDlogPtr)
- DisposeDialog(gMainDlogPtr);
-
- ShutdownHideMenuBar();
-
- }//QuitApp
-
-
- //____ OpenOurModelessDialog __________________________________________________________________________
- //
- // Open our window, install palette built from a resource CTable.
- // provided by the System.
- //
- static Boolean OpenOurModelessDialog( void )
- {
- Rect mainScreenRect;
-
- // create and show the window
- gMainDlogPtr = GetNewDialog( kAmalgamDLOGID, NULL, (WindowRef)-1);
- if (NULL == gMainDlogPtr)
- return FALSE;
-
- mainScreenRect = (**gMainDevice).gdRect;
- mainScreenRect.top += LMGetMBarHeight();
- CenterWindowInRect(gMainDlogPtr, &mainScreenRect, TRUE);
-
- ShowWindow( gMainDlogPtr );
- SetPort( gMainDlogPtr );
-
- gOriginalDlogPortRect = gMainDlogPtr->portRect;
-
- GetGWorld( &gMainDlogPort, &gMainDlogGDev ); //save this info for later convenience
-
- return TRUE;
- }//OpenOurModelessDialog
-
-
- //____ EventLoop __________________________________________________________________________
- //
- // Nifty function shows off some ways to do Palette Animation
- //
- // Returns void
- //
- static void EventLoop( void )
- {
- Boolean done;
-
- done = FALSE;
- while (!done)
- {
- WaitNextEvent(everyEvent, &gTheEvent, kSleepTix, 0L);
-
- HandleEvent();
-
- if (gQuitting)
- {
- done = TRUE;
- }
-
-
- }//while
-
- }//EventLoop
-
-
-
-
- //____ HandleEvent __________________________________________________________________________
- //
- // Handles a single event
- //
- // Returns void
- //
- static void HandleEvent( void )
- {
- #define kSuspendingOrResuming 0x0001
- #define kResuming 1
-
- //intercept events for the modeless dialog here
- if (IsDialogEvent(&gTheEvent))
- {
- if (HandleOurDlogEvent())
- return;
- }
-
- //okay, normal event happend. Ho hum.
- switch (gTheEvent.what)
- {
- case nullEvent:
- break;
-
- case mouseDown:
- HandleMouseDown();
- break;//mouseDown
-
- case keyDown:
- case autoKey:
- HandleKey();
- break;
-
- case updateEvt:
- break;
-
-
- case osEvt:
- switch ((gTheEvent.message & osEvtMessageMask) >> 24)
- {
- case mouseMovedMessage:
- break;
-
- case suspendResumeMessage:
- if (gTheEvent.message & resumeFlag)
- {
- gInForeground = TRUE;
-
- }
- else
- {
- gInForeground = FALSE;
- }
-
- CheckMenuHide(NULL);
-
- if (gTheEvent.message & convertClipboardFlag)
- ;
-
- break; //suspendResumeMessage
- }//switch gTheEvent.message & osEvtMessageMask
- break;//osEvt
-
- case diskEvt:
- // This handles a bad disk. Otherwise the disk will not eject.
- if ( gTheEvent.message >> 16 )
- {
- Point tempPoint;
- tempPoint.v = 50; tempPoint.h = 50;
- DIBadMount(tempPoint, gTheEvent.message);
- }
- break;
-
- }//switch (gTheEvent.what)
-
- }//HandleEvent
-
-
-
-
- //____ HandleKey __________________________________________________________________________
- //
- // Handles a keystroke; looks in gTheEvent to find it
- //
- // Returns void
- //
- void HandleKey( void )
- {
- char theChar;
-
- theChar = gTheEvent.message & charCodeMask;
- if ((gTheEvent.modifiers & cmdKey) != 0)
- {
- HandleMenuChoice(MenuKey(theChar));
- return;
- }
-
- //keystroke wasn't a cmd-key combo; could test for
- //regular keys here...
-
- CheckMenuHide(NULL);
-
- return;
- }//HandleKey
-
-
- //____ HandleMouseDown __________________________________________________________________________
- //
- // Normally called from the event loop, this expects mousedown info to be
- // in the gTheEvent global.
- //
- void HandleMouseDown( void )
- {
- WindowPtr whichWindow;
- short int thePart;
- long menuChoice;
- Rect windowStructRect;
-
- RgnHandle theGrayRgn;
- Rect grayRgnLimitRect;
-
- //if click would be in menubar, show the menubar if hidden
- CheckMenuHide(&gTheEvent.where);
-
- thePart = FindWindow(gTheEvent.where, &whichWindow);
-
- //did user click anywhere in a window? If so, if it's not front window, bring it
- //to the fore
- if (NULL != whichWindow)
- {
- windowStructRect = mWindStructRect(whichWindow);
- if ((PtInRect(gTheEvent.where, &windowStructRect)) && (whichWindow != FrontWindow()))
- SelectWindow(whichWindow);
- }
-
- switch(thePart)
- {
- case inMenuBar:
- menuChoice = MenuSelect(gTheEvent.where);
- HandleMenuChoice(menuChoice);
- break;
-
- case inSysWindow:
- SystemClick(&gTheEvent, whichWindow);
- break;
-
- case inContent:
- break;
-
- case inDrag:
- //was click in titleBar of our app? Let's drag it around, shall we?
- if (whichWindow == gMainDlogPtr)
- {
- theGrayRgn = GetGrayRgn(); //get region of all active screens
- //region changes when screens are moved
- //or the menu bar is hidden/unhidden
- grayRgnLimitRect = (**theGrayRgn).rgnBBox;
- if (inDrag == thePart)
- {
- DragWindow( gMainDlogPtr, gTheEvent.where, &grayRgnLimitRect);
- }
- }
-
- break;
-
- case inGrow:
- break;
-
- case inZoomIn: //use wants to shrink window
- case inZoomOut: //user wants to grow window
- if ((whichWindow == gMainDlogPtr) &&
- (TrackBox( whichWindow, gTheEvent.where, thePart )))
- ZoomOurDialog( thePart );
- break;
-
- case inGoAway:
- if ((whichWindow == gMainDlogPtr) &&
- (TrackGoAway(whichWindow, gTheEvent.where)))
- {
- gQuitting = TRUE;
- }
-
- break;
- }
-
- CheckMenuHide(NULL);
-
- }//HandleMouseDown
-